<?php

namespace app\admin\model;

use think\facade\Session;
use think\Model;
use think\model\concern\SoftDelete;

class Article extends Model
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $defaultSoftDelete = 0;
    public function category()
    {
        return $this->hasOne(Category::class, 'id', 'cate_id');
    }
    public function comments()
    {
        return $this->hasMany(Comment::class, 'art_id', 'id');
    }
    public function userInfo()
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }
    public function setImagesAttr($val)
    {
        if (!is_array($val)) {
            return explode(',', $val);
        }
    }
    public static function verifyCategoryAdmin($art_id)
    {
        $session = Session::get('admin');
        $uid = $session['id'];
        $role = $session['role'];
        $cate_id = CategoryAdmin::getChildCateId($uid);
        $art = self::where(['id' => $art_id])->find();
        if (!in_array($art['cate_id'], $cate_id) && $role != 0) {
            return false;
        }
        return $art;
    }
}
